package com.liaoct.lct.activity;

import java.util.ArrayList;
import java.util.List;
import com.liaoct.lct.adapter.ViewPagerAdapter;
import com.liaoct.lct.listener.RoundDotClickListener;
import com.liaoct.lct.listener.ViewPagerOnPageChangListener;
import android.os.Bundle;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.ImageView;

/**
 * 启动引导页Activity
 * 
 * @author liaoct
 * 
 */
public class GuideActivity extends BaseActivity {

	/**
	 * 布局加载器
	 */
	private LayoutInflater inflater;

	/**
	 * 存放ViewPager中View的List
	 */
	private List<View> pagerViewList;

	/**
	 * 大图片展示控件
	 */
	private ViewPager viewPager;

	/**
	 * 小圆点ImageView存放List
	 */
	private List<ImageView> pageList = null;

	/**
	 * 摆放小圆点的ViewGroup
	 */
	private ViewGroup viewGroup;

	/**
	 * 初始化
	 */
	private void init() {
		inflater = this.getLayoutInflater();
		pageList = new ArrayList<ImageView>();
		pagerViewList = new ArrayList<View>();
		int[] imageIds = { R.drawable.logo1, R.drawable.logo2,
				R.drawable.logo3, R.drawable.logo4 };

		viewPager = (ViewPager) this.findViewById(R.id.carouselViewPager);
		for (int i = 0; i < imageIds.length; i++) {
			View view = inflater.inflate(R.layout.carouse_image_view, null);
			ImageView imageView = (ImageView) view
					.findViewById(R.id.carouseImageView);
			imageView.setBackgroundResource(imageIds[i]);
			pagerViewList.add(view);

		}

		viewGroup = (ViewGroup) this.findViewById(R.id.carouselViewGroup);
		for (int i = 0; i < pagerViewList.size(); i++) {
			ImageView image = new ImageView(this);
			image.setPadding(20, 0, 20, 0);
			image.setScaleType(ImageView.ScaleType.FIT_CENTER);
			image.setLayoutParams(new LayoutParams(40, 40));
			image.setOnClickListener(new RoundDotClickListener(i, viewPager));
			if (i == 0) {
				image.setBackgroundResource(R.drawable.select);
			} else {
				image.setBackgroundResource(R.drawable.unselect);
			}
			pageList.add(image);
			viewGroup.addView(image);
			ImageView spaceImage = new ImageView(this);
			spaceImage.setPadding(20, 0, 20, 0);
			spaceImage.setScaleType(ImageView.ScaleType.FIT_CENTER);
			spaceImage.setLayoutParams(new LayoutParams(40, 40));
			viewGroup.addView(spaceImage);
		}
		ViewPagerAdapter viewAdapter = new ViewPagerAdapter(pagerViewList);
		viewPager.setAdapter(viewAdapter);
		ViewPagerOnPageChangListener viewListener = new ViewPagerOnPageChangListener(
				this, pageList);
		viewPager.setOnPageChangeListener(viewListener);
	}

	@Override
	public void nextActivity() {
		Intent intent = new Intent(GuideActivity.this, LoginActivity.class);
		this.startActivity(intent);
		this.finish();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_guide);
		this.init();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
